
library(tidyverse)
library(fixest)
library(modelsummary)
library(kableExtra)
options(modelsummary_factory_default = 'kableExtra')
options(modelsummary_factory_latex = 'kableExtra')
options(modelsummary_factory_html = 'kableExtra')

sepdata <- readRDS("./sepdata_t2.rds")

#### DESCRIPTIVES ####

sepdata %>%
  select("Cosine" = cosine,
         "GPT index" = index,
         "General-purpose technology 1" = generaltech,
         "General-purpose technology 2" = generaltech2,
         "General-purpose technology 3" = generaltech3,
         "Specialized technology" = spectech,
         "Patents per GDP" = patentgdp,
         "Democracy (polyarchy)" = v2x_polyarchy_VDEM,
         "GDP growth" = GNI_growth_WDI,
         "Scientific and technical journal articles" = journal_WDI,
         "Technicians in R&D (per million people)" = tech_rnd_WDI,
         "Urban population" = upop_MC) %>%
  as.data.frame() %>%
  stargazer::stargazer(type = "latex")

#### MODELS ####

models <- list(feols(cosine ~ index | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata),
               feols(cosine ~ generaltech | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata),
               feols(cosine ~ generaltech2 | year + country,
                     cluster = c("year", "country"), data = sepdata),
               feols(cosine ~ generaltech3 | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata),
               feols(cosine ~ spectech | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata),
               feols(cosine ~ index + patentgdp + v2x_polyarchy_VDEM + GNI_growth_WDI + journal_WDI + tech_rnd_WDI + log(upop_MC) | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata),
               feols(cosine ~ generaltech3 + patentgdp + v2x_polyarchy_VDEM + GNI_growth_WDI + journal_WDI + tech_rnd_WDI + log(upop_MC) | year + country,
                     cluster = c("year", "country", "tech"), data = sepdata))
names(models) <- c("Index of GPT", "GPT 1", "GPT 2", "GPT 3", "Specialized technology", "Index of GPT + control", "GPT 3 + control")

cm <- c("index" = "GPT index",
        "generaltech" = "GPT classification",
        "generaltech2" = "GPT classification",
        "generaltech3" = "GPT classification",
        "spectech" = "Classification",
        "patentgdp" = "Patents per resident",
        "v2x_polyarchy_VDEM" = "Democracy (polyarchy)",
        "GNI_growth_WDI" = "GPD growth",
        "journal_WDI" = "Scientific and technical journal articles",
        "tech_rnd_WDI" = "Technicians in R&D (per million people)",
        "govexp_edu_pexp_WDI" = "Government expenditure on education, total (% of GDP)",
        "log(upop_MC)" = "Urban population",
        "irst_MC" = "Iron and steel production")

modsum <- modelsummary::modelsummary(models,
                                     fmt = 3,
                                     coef_map = cm,
                                     stars = TRUE,
                                     output = "latex",
                                     gof_omit = 'AIC|BIC|Within|Std.Errors|FE',
                                     notes = c("Fixed effects by country and year. Standard errors clustered by country, year and technology."))

modsum %>%
  add_header_above(c(" " = 1, "Cosine similarity between patents in t-2 and standards in t" = 7)) %>%
  kable_styling(font_size = 13, full_width = F) 
